package com.tree.file;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName com.tree.file
 * Description: <类功能描述>. <br>
 * <p>
 * 替换文件或文件夹中字符串
 * </p>
 * @Author tree
 * @Date 5/8/24 7:51 PM
 * @Version 1.0
 */
public class ReplaceCharacterInFile {
    private static final String FIND_STRING = "https://tree3170.gitee.io/pub-images";
    private static final String REPLACE_AFTER = "../../image";
    private static final String ABS_PATH = "/Users/tree/IdeaProject_Local/interview-new/doc";
    private static final String ABS_PATH_TEST_FILE = "/Users/tree/IdeaProject_Local/interview-new/doc/面试相关题/JVM.md";
    private static final String FILE_SUFFIX = ".md";
    private static List<String> allFiles = new ArrayList();

    public static void main(String[] args) throws IOException {
        File file = new File(ABS_PATH);
        //找出所有的符合的文件
        allFiles.addAll(forLoop(file));
        //重写替换
        reWriteFiles(allFiles);

//        allFiles.forEach(System.out::println);
    }

    /**
     * 重写所有的文件（替换）
     * @param allFiles
     * @throws IOException
     */
    private static void reWriteFiles(List<String> allFiles) throws IOException {
        for (String fileName : allFiles) {
            StringBuffer buffer = new StringBuffer();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            boolean modifiedFlag = false;
            for (String line = ""; (line = bufferedReader.readLine()) != null; line = null) {
                if (line.indexOf(FIND_STRING) != -1) {
                    line = line.replace(FIND_STRING, REPLACE_AFTER);
                    modifiedFlag = true;
                }
                buffer.append(line);
                buffer.append(System.getProperty("line.separator"));
            }
            bufferedReader.close();
            if(modifiedFlag) {
                System.out.println("重写文件:"+fileName);
                PrintWriter pw = new PrintWriter(fileName);
                pw.write(buffer.toString().toCharArray());
                pw.flush();
                pw.close();
            }
        }
    }

    /**
     * 递归找到所有.md结尾的文件
     * @param file
     * @return
     */
    public static List<String> forLoop(File file) {
        List<String> list = new ArrayList<>();
        if(file!= null){
            if(file.isDirectory()) {
                File[] files = file.listFiles();
                if (files != null && files.length > 0) {
                    for (File tmp : files) {
                        if (tmp.isDirectory()) {
                            list.addAll(forLoop(tmp));
                        } else {
                            if (!tmp.getName().equals(FILE_SUFFIX) && tmp.getName().endsWith(FILE_SUFFIX)) {
                                list.add(tmp.getAbsolutePath());
                            }
                        }
                    }
                }
            }else {
                if (!file.getName().equals(FILE_SUFFIX) && file.getName().endsWith(FILE_SUFFIX)) {
                    list.add(file.getAbsolutePath());
                }
            }


        }
        return list;
    }
}
